<?php

// +----------------------------------------------------------------------
// | Easyadmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\user;

use app\admin\model\website\Envelopes as EnvelopesModel;
use app\admin\model\website\EnvelopesRecord;
use app\admin\model\website\Users;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\facade\Db;
use think\App;

use broadcast\redis\RedisRecord;

/**
 * Class Admin
 * @package app\admin\controller\user
 * @ControllerAnnotation(title="站点管理")
 */
class Envelopes extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new EnvelopesModel();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            
            $countModel = $this->model;
            if (session("admin.group") != 1 && session("admin.group") != 5) {
                $countModel = $countModel->where("uid",session('admin.id'));
            }else if (session("admin.group") == 5) {
                $countModel = $countModel->where('roomid','in',session("session.rooms"));
            }
            $count = $countModel->where($where)
                ->count();
            $listModel = $this->model;
            if (session("admin.group") != 1 && session("admin.group") != 5) {
                $listModel = $listModel->where("uid",session('admin.id'));
            }else if (session("admin.group") == 5) {
                $listModel = $listModel->where('roomid','in',session("session.rooms"));
            }
            $list  = $listModel->where($where)->page($page, $limit)
                ->order($this->sort)
                ->select()->each(function ($item) {
                    $item['username'] = Users::where("id",$item['uid'])->value("username");
                });
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function record($id=null)
    {
        if ($id != null) {
            cookie('envelopesid',$id);
        }
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            
            
            $count = (new EnvelopesRecord())
                ->where("eid",cookie('envelopesid'))
                ->where($where)
                ->count();
            $list = (new EnvelopesRecord())
                ->alias("e")
                ->join('users u','u.id = e.uid')
                ->where($where)
                ->where("e.eid",cookie('envelopesid'))
                ->field("u.username,e.*")
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        if ($save){
            $redEnvelopesHandle = new RedisRecord(session("website"),'redEnvelopesHandle');
            $redEnvelopesHandle->delEnvelopes($id);
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        } 
    }

}
